package SingletonPattern;
//懒汉模式，调用的时候创建唯一实例
class SingletonLazy {
    private static volatile SingletonLazy instance1 = null;
    private SingletonLazy(){

    }
    public static SingletonLazy getInstance1() {
        Object object = new Object();
        if(instance1 == null) {
            synchronized(object) {
                if (instance1 == null) {
                    instance1 = new SingletonLazy();
                }
            }
        }
         return instance1;
    }
}
public class LazyMode {
    //懒汉模式在多线程中存在线程安全问题，需要通过加锁保证线程安全
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance1();
        SingletonLazy s2 = SingletonLazy.getInstance1();
        System.out.println(s1 == s2);
    }
}
